package com.qupai.lib_base.utils

import android.graphics.Bitmap
import android.os.Environment
import com.blankj.utilcode.util.ImageUtils
import com.blankj.utilcode.util.Utils
import java.io.File

object MyImageUtils {

    fun save2Album(
        src: Bitmap,
        format: Bitmap.CompressFormat,
        recycle: Boolean = true
    ): File? {
        val safeDirName = Utils.getApp().packageName
        val quality = 100
        val suffix = if (Bitmap.CompressFormat.JPEG == format) "JPG" else format.name
        val fileName = System.currentTimeMillis().toString() + "_" + quality + "." + suffix
        val picDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM)
        val file = File(picDir, "$safeDirName${File.separator}${fileName}")
        val success = ImageUtils.save(src, file, format, quality, recycle)
        return if (success && file.exists()) file else null
    }

    fun save(
        tag:String,
        src: Bitmap,
        format: Bitmap.CompressFormat,
        recycle: Boolean = true
    ): File? {
        val safeDirName = Utils.getApp().packageName
        val quality = 100
        val suffix = if (Bitmap.CompressFormat.JPEG == format) "JPG" else format.name
        val fileName = System.currentTimeMillis().toString() + "_${tag}_" + quality + "." + suffix
        val picDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM)
        val file = File(picDir, "$safeDirName${File.separator}${fileName}")
        val success = ImageUtils.save(src, file, format, quality, recycle)
        return if (success && file.exists()) file else null
    }

}